/*
 * Copyright (C) 2004, Velocityme Partnership
 * ContactDetailTypeActionForm.java
 *
 * Created on 16 October 2004, 10:04
 */

package com.velocityme.www.actionforms;

import com.velocityme.valueobjects.ContactDetailTypeValue;
import com.velocityme.valueobjects.NotificationMechanismValue;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class ContactDetailTypeActionForm extends ActionForm {
    
    public class ContactDetailType implements Serializable {
        private Integer m_contactDetailTypeId;
        private String m_typeName;
        private Integer m_notificationId;
        private Boolean m_isShown = Boolean.FALSE;
        private Boolean m_isShownInDirectory = Boolean.FALSE;
        
        public Integer getContactDetailTypeId() { return m_contactDetailTypeId; }
        public void setContactDetailTypeId(Integer contactDetailTypeId) { m_contactDetailTypeId = contactDetailTypeId; }
        public String getTypeName() { return m_typeName; }
        public void setTypeName(String typeName) { m_typeName = typeName; }
        public Integer getNotificationId() { return m_notificationId; }
        public void setNotificationId(Integer notificationId) { m_notificationId = notificationId; }
        public Boolean getIsShown() { return m_isShown; }
        public void setIsShown(Boolean isShown) { m_isShown = isShown; }
        public Boolean getIsShownInDirectory() { return m_isShownInDirectory; }
        public void setIsShownInDirectory(Boolean isShownInDirectory) { m_isShownInDirectory = isShownInDirectory; }
    }
    
    private ArrayList m_contactDetailTypeList;
    private int m_selectedValue = 0;
    private String m_cmd="";
    
    /** Creates a new instance of ContactDetailTypeActionForm */
    public ContactDetailTypeActionForm() {
        m_contactDetailTypeList = new ArrayList();
    }
    
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
        Iterator i = m_contactDetailTypeList.iterator();
        while(i.hasNext()) {
            ContactDetailType contactDetailType = (ContactDetailType)i.next();
            contactDetailType.setIsShown(Boolean.FALSE);
            contactDetailType.setIsShownInDirectory(Boolean.FALSE);
        }
    }
    
    public void setCmd(int index, String value) {
        m_selectedValue = index;
        m_cmd = value;
    }
    
    public String getCmd(int index) {
        return m_cmd;
    }
    
    public String getCmd() {
        return m_cmd;
    }
    
    public int getIndex() {
        return m_selectedValue;
    }
    
    public void setContactDetailTypeValues(Collection contactDetailTypeValues) {
        m_contactDetailTypeList.clear();
        // we assume that the contact detail type values are already sorted by
        // sequence number!
        Iterator i = contactDetailTypeValues.iterator();
        while(i.hasNext()) {
            ContactDetailTypeValue contactDetailTypeValue = (ContactDetailTypeValue)i.next();
            ContactDetailType contactDetailType = new ContactDetailType();
            contactDetailType.m_contactDetailTypeId = contactDetailTypeValue.getContactDetailTypeId();
            contactDetailType.m_typeName = contactDetailTypeValue.getName();
            contactDetailType.m_notificationId = contactDetailTypeValue.getNotificationMechanismValue().getNotificationMechanismId();
            contactDetailType.m_isShown = contactDetailTypeValue.getShowType();
            contactDetailType.m_isShownInDirectory = contactDetailTypeValue.getShowInDirectory();
            m_contactDetailTypeList.add(contactDetailType);
        }
    }
    
    public Collection getContactDetailTypeValues(NotificationMechanismValue[] notificationValues) {
        Map notificationValueMap = new HashMap();
        for (int i = 0; i < notificationValues.length; i++) {
            notificationValueMap.put(notificationValues[i].getNotificationMechanismId(), notificationValues[i]);
        }
        Collection typeValues = new ArrayList();
        Iterator i = m_contactDetailTypeList.iterator();
        int sequenceNumber = 0;
        while(i.hasNext()) {
            ContactDetailType contactDetailType = (ContactDetailType)i.next();
            ContactDetailTypeValue contactDetailTypeValue = new ContactDetailTypeValue();
            if (contactDetailType.getContactDetailTypeId() != null)
                contactDetailTypeValue.setContactDetailTypeId(contactDetailType.getContactDetailTypeId());
            contactDetailTypeValue.setName(contactDetailType.getTypeName());
            contactDetailTypeValue.setShowType(contactDetailType.getIsShown());
            contactDetailTypeValue.setShowInDirectory(contactDetailType.getIsShownInDirectory());
            contactDetailTypeValue.setSequenceNumber(new Integer(sequenceNumber++));
            contactDetailTypeValue.setNotificationMechanismValue((NotificationMechanismValue)notificationValueMap.get(contactDetailType.getNotificationId()));
            typeValues.add(contactDetailTypeValue);
        }
        return typeValues;
    }
    
    public boolean processCmd() {
        if (m_cmd.equals("Insert")) {
            m_contactDetailTypeList.add(m_selectedValue, new ContactDetailType());
            return true;
        }
        if (m_cmd.equals("Delete")) {
            m_contactDetailTypeList.remove(m_selectedValue);
            return true;
        }
        if (m_cmd.equals("Move Up")) {
            if (m_selectedValue > 0) {
                Object obj = m_contactDetailTypeList.remove(m_selectedValue-1);
                m_contactDetailTypeList.add(m_selectedValue, obj);
            }
            return true;
        }
        return false;
    }
    
    public ArrayList getContactDetailTypeList() {
        return m_contactDetailTypeList;
    }
    
    public void setContactDetailTypeList(ArrayList contactDetailTypeList) {
        m_contactDetailTypeList = contactDetailTypeList;
    }
    
}
